﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class ReceiptRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15131;
        protected GridView gvReceiptRpt;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected Label lblTotalCube;
        protected Label lblTotalCubeText;
        protected Label lblTotalDiff;
        protected Label lblTotalDiffQty;
        protected Label lblTotalEeceived;
        protected Label lblTotalEeceivedQty;
        protected Label lblTotalExpected;
        protected Label lblTotalExpectedQty;
        protected Label lblTotalGrossWGT;
        protected Label lblTotalGrossWGTText;
        protected Label lblTotalWgt;
        protected Label lblTotalWgtText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalCube;
        private decimal TotalDiffQty;
        private decimal TotalEeceivedQty;
        private decimal TotalExpectedQty;
        private decimal TotalGrossWGT;
        private decimal TotalWgt;
        protected UpdatePanel up1;

        private void BindData2gvReceiptRptList(ReceiptRptQueryEntity receiptRptQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(receiptRptQuery.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetReceiptRptInfo(receiptRptQuery);
            base.BindData2GridView(this.gvReceiptRpt, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ReceiptRptQueryEntity receiptRptQuery = this.GetReceiptRptQueryEntity();
            receiptRptQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = receiptRptQuery.CurrentPage;
            receiptRptQuery.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetReceiptRptInfo(receiptRptQuery))
                {
                    base.Export2Excel("ReceiptRpt.xls", "15131", this.gvReceiptRpt, ds);
                }
            }
            finally
            {
                receiptRptQuery.IsGetAll = false;
                receiptRptQuery.CurrentPage = currentPageIndex;
                this.SetReceiptRptQueryEntity(receiptRptQuery);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ReceiptRptQueryEntity receiptRptQuery = this.GetReceiptRptQueryEntity();
            receiptRptQuery.CurrentPage = "0";
            this.BindData2gvReceiptRptList(receiptRptQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            ReceiptRptQueryEntity receiptRptQuery = this.GetReceiptRptQueryEntity();
            receiptRptQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2gvReceiptRptList(receiptRptQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            ReceiptRptQueryEntity receiptRptQuery = this.GetReceiptRptQueryEntity();
            receiptRptQuery.RECEIPT_ID = "";
            receiptRptQuery.EXTERNAL_RECEIPT_ID = "";
            receiptRptQuery.PO_ID = "";
            receiptRptQuery.OWNER_ID = "";
            receiptRptQuery.RECEIPT_TYPE = "";
            receiptRptQuery.StartDateTime = "";
            receiptRptQuery.EndDateTime = "";
            receiptRptQuery.SKU_CLASS_ID = "";
            receiptRptQuery.SKU_ID = "";
            receiptRptQuery.VENDOR_ID = "";
            receiptRptQuery.RMA = "";
            receiptRptQuery.Descr = "";
            this.BindData2gvReceiptRptList(receiptRptQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            ReceiptRptQueryEntity receiptRptQuery = this.GetReceiptRptQueryEntity();
            if (int.Parse(receiptRptQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                receiptRptQuery.CurrentPage = (int.Parse(receiptRptQuery.CurrentPage) + 1).ToString();
                this.BindData2gvReceiptRptList(receiptRptQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            ReceiptRptQueryEntity receiptRptQuery = this.GetReceiptRptQueryEntity();
            if (int.Parse(receiptRptQuery.CurrentPage) > 0)
            {
                receiptRptQuery.CurrentPage = (int.Parse(receiptRptQuery.CurrentPage) - 1).ToString();
                this.BindData2gvReceiptRptList(receiptRptQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            ReceiptRptQueryEntity receiptRptQuery = this.GetReceiptRptQueryEntity();
            this.BindData2gvReceiptRptList(receiptRptQuery);
        }

        private ReceiptRptQueryEntity GetReceiptRptQueryEntity()
        {
            return (base.PageQueryEntitySession as ReceiptRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                ReceiptRptQueryEntity receiptRptQuery = this.GetReceiptRptQueryEntity();
                receiptRptQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2gvReceiptRptList(receiptRptQuery);
            }
        }

        protected void gvReceiptRpt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15131", this.gvReceiptRpt);
        }

        protected void gvReceiptRpt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (((Label) e.Row.FindControl("lblEeceivedQty")) != null)
                {
                    this.TotalEeceivedQty += decimal.Parse(((Label) e.Row.FindControl("lblEeceivedQty")).Text);
                }
                if (((Label) e.Row.FindControl("lblDiffQty")) != null)
                {
                    this.TotalDiffQty += decimal.Parse(((Label) e.Row.FindControl("lblDiffQty")).Text);
                }
                if (((Label) e.Row.FindControl("lblExpectedQty")) != null)
                {
                    this.TotalExpectedQty += decimal.Parse(((Label) e.Row.FindControl("lblExpectedQty")).Text);
                }
                if (((Label) e.Row.FindControl("lblTotalWgt")) != null)
                {
                    this.TotalWgt += decimal.Parse(((Label) e.Row.FindControl("lblTotalWgt")).Text);
                }
                if (((Label) e.Row.FindControl("lblTotalGrossWgt")) != null)
                {
                    this.TotalGrossWGT += decimal.Parse(((Label) e.Row.FindControl("lblTotalGrossWgt")).Text);
                }
                if (((Label) e.Row.FindControl("lblTotalCube")) != null)
                {
                    this.TotalCube += decimal.Parse(((Label) e.Row.FindControl("lblTotalCube")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalDiffQty.Text = base.GetPreferenceDigit(this.TotalDiffQty);
                this.lblTotalEeceivedQty.Text = base.GetPreferenceDigit(this.TotalEeceivedQty);
                this.lblTotalExpectedQty.Text = base.GetPreferenceDigit(this.TotalExpectedQty);
                this.lblTotalCubeText.Text = base.GetPreferenceDigit(this.TotalCube);
                this.lblTotalGrossWGTText.Text = base.GetPreferenceDigit(this.TotalGrossWGT);
                this.lblTotalWgtText.Text = base.GetPreferenceDigit(this.TotalWgt);
            }
        }

        protected void gvReceiptRpt_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                ReceiptRptQueryEntity receiptRptQuery = this.GetReceiptRptQueryEntity();
                receiptRptQuery.SortField = e.SortExpression;
                if ((receiptRptQuery.SortDirection == "ASC") || (receiptRptQuery.SortDirection.Trim() == ""))
                {
                    receiptRptQuery.SortDirection = "DESC";
                }
                else if (receiptRptQuery.SortDirection == "DESC")
                {
                    receiptRptQuery.SortDirection = "ASC";
                }
                this.BindData2gvReceiptRptList(receiptRptQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            ReceiptRptQueryEntity receiptRptQuery = new ReceiptRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    receiptRptQuery.RECEIPT_ID = this.PreviousPage.ReceiptID;
                    receiptRptQuery.EXTERNAL_RECEIPT_ID = this.PreviousPage.ExternalReceiptID;
                    receiptRptQuery.PO_ID = this.PreviousPage.PoID;
                    receiptRptQuery.OWNER_ID = this.PreviousPage.OwnerID;
                    receiptRptQuery.RECEIPT_TYPE = this.PreviousPage.ReceiptType;
                    receiptRptQuery.StartDateTime = this.PreviousPage.StartDateTime;
                    receiptRptQuery.EndDateTime = this.PreviousPage.EndDateTime;
                    receiptRptQuery.SKU_CLASS_ID = this.PreviousPage.SkuClassID;
                    receiptRptQuery.SKU_ID = this.PreviousPage.SkuID;
                    receiptRptQuery.VENDOR_ID = this.PreviousPage.VendorID;
                    receiptRptQuery.RMA = this.PreviousPage.Rma;
                    receiptRptQuery.Descr = this.PreviousPage.Descr;
                    receiptRptQuery.EXTERNAL_LOT = "";
                }
                else
                {
                    receiptRptQuery.RECEIPT_ID = "";
                    receiptRptQuery.EXTERNAL_RECEIPT_ID = "";
                    receiptRptQuery.PO_ID = "";
                    receiptRptQuery.OWNER_ID = "";
                    receiptRptQuery.RECEIPT_TYPE = "";
                    receiptRptQuery.StartDateTime = "";
                    receiptRptQuery.EndDateTime = "";
                    receiptRptQuery.SKU_CLASS_ID = "";
                    receiptRptQuery.SKU_ID = "";
                    receiptRptQuery.VENDOR_ID = "";
                    receiptRptQuery.RMA = "";
                    receiptRptQuery.Descr = "";
                    receiptRptQuery.EXTERNAL_LOT = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            receiptRptQuery.IsGetAll = false;
            receiptRptQuery.CurrentPage = "0";
            receiptRptQuery.PageSize = this.gvReceiptRpt.PageSize.ToString();
            receiptRptQuery.SortField = "RECEIPT_ID DESC";
            receiptRptQuery.SortDirection = "";
            this.PagerFooter.PageSize = this.gvReceiptRpt.PageSize;
            this.SetReceiptRptQueryEntity(receiptRptQuery);
            this.BindData2gvReceiptRptList(receiptRptQuery);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvReceiptRpt);
                this.LoadData();
            }
        }

        private void SetReceiptRptQueryEntity(ReceiptRptQueryEntity receiptRptQuery)
        {
            base.PageQueryEntitySession = receiptRptQuery;
        }

        public ReceiptRptQuery PreviousPage
        {
            get
            {
                return (ReceiptRptQuery) base.PreviousPage;
            }
        }
    }
}

